package org.smart4j.chapter2.controller;


import org.smart4j.chapter2.model.Customer;
import org.smart4j.chapter2.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/customer_addto")
public class CustomerAddServlet extends HttpServlet {

    private CustomerService customerService;

    @Override
    public void init() throws ServletException{
        customerService=new CustomerService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("aa");
        Map<String, String[]> map1 = req.getParameterMap();
        Map map2 = new HashMap();  //第二种方式
        Set<Map.Entry<String, String[]>> entries = map1.entrySet();
        for (Map.Entry<String, String[]> set:entries){
            System.out.println(set.getKey()+"  :  "+set.getValue()[0]);
            map2.put(set.getKey(),set.getValue()[0]);
        }
        Map<String,Object> map = new HashMap<>(); //第一种方式
        map.put("name",req.getParameter("name"));
        map.put("contact",req.getParameter("contact"));
        map.put("telephone",req.getParameter("telephone"));
        map.put("email",req.getParameter("email"));
        map.put("remark",req.getParameter("remark"));
        customerService.createCustomer(map2);
        req.getRequestDispatcher("/customer").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
